#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
#include <unordered_map>
using namespace std;
#include <algorithm>

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals)
    {
        sort(intervals.begin(), intervals.end(), [](vector<int> a, vector<int> b) {
            return a[0] < b[0];
            });

        vector<vector<int>> ret;
        for (int i = 0; i < intervals.size(); i++)
        {
            int left = intervals[i][0], right = intervals[i][1];
            if (ret.empty() || ret.back()[1] < left)
            {
                ret.push_back({ left,right });
            }
            else
            {
                ret.back()[1] = max(ret.back()[1], right);
            }
        }
        return ret;
    }
};

class Solution {
public:
    int findMaxLength(vector<int>& nums)
    {
        int len = 0;
        unordered_map<int, int> hash;
        hash[0] = -1;
        int sum = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] == 0)
                sum--;
            else
                sum++;

            if (hash.count(sum))
                len = max(len, i - hash[sum]);
            else
                hash[sum] = i;
        }
        return len;
    }
};